package com.molichuxing.gateway.api.services.impl;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.result.ResponseResult;
import com.molichuxing.framework.utils.DateUtil;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.gateway.api.config.OptionalLogConfig;
import com.molichuxing.gateway.api.services.RiskApiService;
import com.molichuxing.gateway.bff.risk.entity.request.create.BlacklistCreateVo;
import com.molichuxing.gateway.bff.risk.entity.request.modify.BlacklistModifyVo;
import com.molichuxing.gateway.bff.risk.entity.response.BlacklistVo;
import com.molichuxing.gateway.bff.risk.entity.response.CreditRecognitionVo;
import com.molichuxing.gateway.bff.risk.services.BlacklistBffService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * @author liyg
 * 风控web接口实现类
 * 2019-08-31
 *
 */
@Api(tags = "风控相关接口")
@RestController
@RequestMapping(value = "/risk")
public class RiskApiServiceImpl extends RootApiService implements RiskApiService{

	private static final Logger logger = LoggerFactory.getLogger(RiskApiServiceImpl.class);

	@Resource
	private BlacklistBffService blacklistBffService;
	
	/**
	 *获取黑名单列表
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取黑名单列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userName", value = "用户名", required = false, dataType = "String"),
			@ApiImplicitParam(name = "name", value = "用户姓名", required = false, dataType = "String"),
			@ApiImplicitParam(name = "phone", value = "用户手机号", required = false, dataType = "String"),
			@ApiImplicitParam(name = "idNumber", value = "身份证号", required = false, dataType = "String"),
			@ApiImplicitParam(name = "startTime", value = "拉黑开始时间", required = false, dataType = "String"),
			@ApiImplicitParam(name = "endTime", value = "拉黑结束时间", required = false, dataType = "String"),
			@ApiImplicitParam(name = "type", value = "拉黑方式", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "Integer")

	})
	@GetMapping(value = "/blacklist/list")
	public ResponseResult<Paged<BlacklistVo>> getBlacklistList(@RequestParam Map<String, Object> map) {
		ResponseResult<Paged<BlacklistVo>> result = new ResponseResult<>();
		try {
			Integer pageNum = ObjectCastUtil.castInteger(map.get("pageNum"));
			if(null==pageNum || pageNum < 1 ) {
				map.put("pageNum",1);
			}
			Integer pageSize = ObjectCastUtil.castInteger(map.get("pageSize"));
			if(null==pageSize || pageSize <= 0) {
				map.put("pageSize",10);
			}
			String startTime = ObjectCastUtil.castString(map.get("startTime"));
			if(StringUtils.isNoneBlank(startTime)) {
				map.put("startTime",DateUtil.parseDate(startTime).atStartOfDay());
			}
			String endTime = ObjectCastUtil.castString(map.get("endTime"));
			if(StringUtils.isNoneBlank(endTime)) {
				map.put("endTime", DateUtil.getDayEnd(DateUtil.parseDate(endTime).atStartOfDay()));
			}
			
			return result.success(blacklistBffService.getBlacklistList(map));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getBlacklistList]获取黑名单列表异常", e);
			result.error("获取黑名单列表异常");
		} 
		return result;
	}
	
	/**
	 * 获取用户征信信息
	 * @param
	 * @return
	 */
	@Override
	@ApiOperation(value = "获取用户征信信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Integer")
	})
	@GetMapping(value = "/credit/detail")
	public ResponseResult<CreditRecognitionVo> getCreditRecognition(@RequestParam Integer userId) {
		ResponseResult<CreditRecognitionVo> result = new ResponseResult<>();
		try {
			return result.success( blacklistBffService.getCreditRecognition(userId));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getCreditRecognition]获取用户征信信息异常", e);
			result.error("获取用户征信信息异常");
		} 
		return result;
	}
	
	/**
	 * 创建黑名单
	 * @param createVo
	 * @return
	 * 
	 */
	@Override
	@ApiOperation(value = "创建黑名单")
	@OptionalLogConfig(methods="拉黑")
	@PostMapping(value = "/blacklist/create")
	public ResponseResult<Boolean> createBlacklist(@RequestBody BlacklistCreateVo createVo){
		ResponseResult<Boolean>  result = new ResponseResult<>();
		try {
			return result.success(blacklistBffService.createBlacklist(createVo));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[createBlacklist]创建黑名单异常", e);
			result.error("创建黑名单异常");
		}
	
		return result;
	}
	
	/**
	 * 用户列表-移除黑名单
	 * @param modifyVo
	 * @return
	 * 
	 */
	@Override
	@ApiOperation(value = "用户列表-移除黑名单")
	@OptionalLogConfig(methods="移除黑名单")
	@PostMapping(value = "/user/blacklist/delete")
	public ResponseResult<Boolean> deleteUserBlacklist(@RequestBody BlacklistModifyVo modifyVo){
		ResponseResult<Boolean>  result = new ResponseResult<>();
		try {
			return result.success(blacklistBffService.deleteUserBlacklist(modifyVo));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[deleteUserBlacklist]移除黑名单异常", e);
			result.error("移除黑名单异常");
		}
		return result;
	}
	
	/**
	 * 移除黑名单
	 * @param modifyVo
	 * @return
	 * 
	 */
	@Override
	@ApiOperation(value = "移除黑名单")
	@OptionalLogConfig(methods="移除黑名单")
	@PostMapping(value = "/blacklist/delete")
	public ResponseResult<Boolean> deleteBlacklist(@RequestBody BlacklistModifyVo modifyVo){
		ResponseResult<Boolean>  result = new ResponseResult<>();
		try {
			return result.success(blacklistBffService.deleteBlacklist(modifyVo));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[deleteBlacklist]移除黑名单异常", e);
			result.error("移除黑名单异常");
		}
		
		return result;
	}
}
